package com.zhu.wx.facade;

import com.google.gson.Gson;
import com.zhu.wx.dict.WxEventEnum;
import com.zhu.wx.dict.WxRecEnum;
import com.zhu.wx.entity.ResMsg;
import com.zhu.wx.factory.Factory;
import com.zhu.wx.factory.StrategyFactory;
import com.zhu.wx.strategy.RecStrategyContext;
import com.zhu.wx.util.WxParseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 外观
 * 作用：外观模式（Facade Pattern）隐藏系统的复杂性，
 * 并向客户端提供了一个客户端可以访问系统的接口。
 */
@Component
public class RecStrategyFacade {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 外观模式不参与子系统的逻辑，所以对子系统进行了一次封装
     */
    private Factory factory = new StrategyFactory();
    private RecStrategyContext context = new RecStrategyContext(null);

    public RecStrategyFacade() {
    }

    public ResMsg execute(Map<String, String> params, WxRecEnum strategy) {
        this.context.setStrategy(this.factory.createStrategy(strategy));
        logger.info("处理请求策略 - {}", strategy.getDesc());
        ResMsg execute = this.context.execute(WxParseUtil.parseToRecMsg(params));
        logger.info("{} - 处理请求结果 - {}", strategy.getDesc(), new Gson().toJson(execute));
        return execute;
    }

    public ResMsg execute(Map<String, String> params, WxEventEnum strategy) {
        this.context.setStrategy(this.factory.createStrategy(strategy));
        logger.info("处理请求策略 - {}", strategy.getDesc());
        ResMsg execute = this.context.execute(WxParseUtil.parseToRecMsg(params));
        logger.info("{} - 处理请求结果 - {}", strategy.getDesc(), new Gson().toJson(execute));
        return execute;
    }

}
